﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using WpfApplication1.Models;
using WpfApplication1.Commands;
using System.Windows.Input;

namespace WpfApplication1.ViewModel
{
    public class FriendViewModel : INotifyPropertyChanged
    {
        CFacebookServer fbServer;

        private FriendModel frienddata;
        

        private string _gender = "";
        private string _birthday = "";
        private string _education = "";

        public string Name
        {
            get { return frienddata.Name; }
            set
            {
                frienddata.Name = value;
                OnPropertyChanged("Name");
            }
        }

        public BitmapImage ProfilePicture
        {
            get
            {
                BitmapImage bm = new BitmapImage();
                bm.BeginInit();
                string urlPic = string.Format("https://graph.facebook.com/{0}/picture?type={1}", frienddata.Id, "square");
                bm.UriSource = new Uri(urlPic);
                bm.EndInit();
                return bm;
            }
        }

        public string Gender
        {
            get { return _gender; }
            set
            {
                _gender = value;
                OnPropertyChanged("Gender");
            }
        }

        public string Birthday
        {
            get { return _birthday; }
            set
            {
                _birthday = value;
                OnPropertyChanged("Birthday");
            }
        }

        public string Id
        {
            get { return frienddata.Id; }
        }

        public string Education
        {
            get { return _education; }
            set
            {
                _education = value;
                OnPropertyChanged("Education");
            }
        }

        public FriendViewModel(string id, string name)
        {
            frienddata = new FriendModel(id, name);
            fbServer = new CFacebookServer(MainWindow.AccessToken);
        }

        public Command FriendClick
        {
            get
            {
                return new Command(GetFriendInfo);
            }
        }

        public Command LinkClick
        {
            get { return new Command(DoLink); }
        }


        public void GetFriendInfo()
        {
            IDictionary<string, object> data = fbServer.GetFriendInfo(frienddata.Id);
            if (data.ContainsKey("gender"))
            {
                this.Gender = (string)data["gender"];
            }
            if (data.ContainsKey("birthday"))
            {
                this.Birthday = (string)data["birthday"];
            }
            if (data.ContainsKey("education"))
            {
                string edu = "";
                List<object> temp = (List<object>)data["education"];
                for (int i = 0; i < temp.Count; i++)
                {
                    IDictionary<string, object> temp2 = (IDictionary<string, object>)temp[i];
                    IDictionary<string, object> temp3 = (IDictionary<string, object>)temp2["school"];
                    string temp4 = (string)temp3["name"];
                    edu += "\n+ " + temp4;
                }
                Education = edu.Substring(1);
            }
        }

        public void DoLink()
        {
            string link = "http://www.facebook.com/profile.php?id=" + frienddata.Id;
            System.Diagnostics.Process.Start(link);
        }


        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
